/**
 * @class NG.models.Fields
 * @extends Ext.data.Model
 * FAD Fields
 */
NG.models.Field = Ext.regModel("NG.models.Field", {
	
	/**
	 * Fields: <br>
	 * <li>fieldName
	 * <li>displayName
	 * <li>fieldType
	 * <li>isKey
	 * <li>mimeType
	 * <li>isLookUp
	 * <li>lookUpProps
	 * <li>visibility
	 * 
	 * @property fields
	 * @type Array
	 */
	fields: [
        {name: "fieldName", type: 'string'},
        {name: "displayName", type: 'string'},
        {name: "fieldType", type: 'string'},
        {name: "isKey", type: 'boolean'},
        {name: "mimeType", type: 'string'},
        {name: "isLookUp", type: 'boolean'},
        {name: "lookUpProps", type: 'string'},
        {name: "visibility", defaultValue: true},
        {name: 'comboProperties', defaultValue: {
        	fadName: '',
        	keyFieldName: '',
        	displayFieldName: ''
        }}
   	],
   	
   	/**
   	 * @private
   	 */
   	phantom: true,
   	
   	/**
   	 * Parses lookupprops (if possible) then fulfills combo properties
   	 * 
   	 * @return {Object} fulfilled combo properties
   	 */
   	getComboProperties: function (){
   		if (!this.isLookUp()) {
   			return '';
   		}
   		if (this.data.comboProperties.fadName == '') {
   			var unparsedLookupProp = this.data.lookUpProps;
   			var parsedLookupProp = unparsedLookupProp.split(';');
   			var unparsedFadName = parsedLookupProp[0];
   			var parsedFadName = unparsedFadName.split('.');
   			this.data.comboProperties.fadName = parsedLookupProp[0];
   			this.data.comboProperties.keyFieldName = parsedLookupProp[1];
   			this.data.comboProperties.displayFieldName = parsedLookupProp[2];
   		}
   		return this.data.comboProperties;
   	},
   	/**
   	 * This array can be used whether field is combo
   	 * 
   	 * @return {Array} obtained combo list values
   	 */
   	createComboItemList: function (geometryMember, comboList){
   		Ext.dispatch({
			controller: app.controllers.infoView,
			action: 'prepareComboListItems',
			field: this,
			geometry: geometryMember,
			component: comboList
		});  
   	},
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getFieldName: function (){return this.data.fieldName;},
   	
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getDisplayName: function (){return this.data.displayName;},
   	
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getFieldType: function (){return this.data.fieldType;},
   	
   	/**
   	 * 
   	 * @return {Boolean}
   	 */
   	isKey: function() {return (this.data.isKey == 1);},
   	
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getMimeType: function() {return this.data.mimeType;},
   	
   	/**
   	 * 
   	 * @return {Boolean}
   	 */
   	isLookUp: function() {return (this.data.isLookUp === 1 || this.data.isLookUp === true);},
   	
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getLookUpProps: function() {return this.data.lookUpProps;},
   	
   	/**
   	 * This method is called when a model is being listed in a List
   	 * 
   	 * @return {String} Text when field is being listed in a List view 
   	 */
   	getTableDisplayValue: function (){
   		return this.getFieldName();
   	},
   	
   	/**
   	 * This method is used for checking field type declared as Date 
   	 * 
   	 * @return {Boolean} true when field type is Date otherwise false
   	 */
   	isDate: function (){
   		return (this.data.fieldType == 'ftDateTime');
   	},
   	
   	/**
   	 * This method is used for checking field type declared as Float
   	 * 
   	 * @return {Boolean} true when field type is Float otherwise false
   	 */
   	isFloat: function (){
   		return (this.data.fieldType == 'ftFloat');
   	},
   	
   	/**
   	 * This method is used for checking field type declared as Integer
   	 * 
   	 * @return {Boolean} true when field type is Integer otherwise false
   	 */
   	isInteger: function (){
   		return (this.data.fieldType == 'ftInteger');
   	},
   	
   	/**
   	 * This method is used for checking field type declared as SmallInt
   	 * 
   	 * @return {Boolean} true when field type is SmallInt otherwise false
   	 */
   	isSmallInt: function (){
   		return (this.data.fieldType == 'ftSmallint');
   	},
   	
   	/**
   	 * This method is used for checking field type declared as LargeInt
   	 * 
   	 * @return {Boolean} true when field type is LargeInt otherwise false
   	 */
   	isLargeInt: function (){
   		return (this.data.fieldType == 'ftLargeint');
   	},
   	
   	/**
   	 * This method is used for checking field type declared as String
   	 * 
   	 * @return {Boolean} true when field type is String otherwise false
   	 */
   	isString: function (){
   		return (this.data.fieldType == 'ftString');
   	},
   	
   	/**
   	 * This method is used for checking field type declared as WideString
   	 * 
   	 * @return {Boolean} true when field type is WideString otherwise false
   	 */
   	isWideString: function (){
   		return (this.data.fieldType == 'ftWideString');
   	},
   	
   	/**
   	 * This method is used for checking field type declared as Memo
   	 * 
   	 * @return {Boolean} true when field type is Memo otherwise false
   	 */
   	isMemo: function (){
   		return (this.data.fieldType == 'ftMemo');
   	},
   	
   	/**
   	 * This method is used for checking field type declared as one of the below<br>
   	 * <li>Integer
   	 * <li>Smallint
   	 * <li>Largeint
   	 * 
   	 * @return {Boolean} true when field type is one of the integer types otherwise false
   	 */
   	isInt: function (){
   		return (this.isInteger() || this.isSmallInt() || this.isLargeInt());
   	},
   	
   	/**
   	 * This method is used for checking field type declared as one of the below<br>
   	 * <li>String
   	 * <li>WideString
   	 * 
   	 * @return {Boolean} true when field type is one of the string types otherwise false
   	 */
   	isStr: function (){
   		return (this.isString() || this.isWideString());
   	},
   	/**
   	 * 
   	 * @param geometry
   	 * @return {Array}
   	 */
   	createComboItemArray: function (geometry) {
   		var result = [];
   		
   		return result;
   	}
});

/**
 * @class NG.models.TableLayout
 * @extends Ext.data.Model
 *
 * //TODO Açıklama
 */
NG.models.TableLayout = Ext.regModel("NG.models.TableLayout", {
	
	/**
	 * Fields: <br>
	 * <li>layoutName
	 * <li>viewType
	 * <li>layoutFields
	 * 
	 * @property fields
	 * @type Array
	 */
    fields: [
        {name: "layoutName", type: 'string'},
        {name: "viewType", type: 'string'},
        {name: "layoutFields" }
   	],
   	
   	/**
   	 * @private
   	 */
   	phantom: true,
   	
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getLayoutName: function (){return this.data.layoutName;},
   	
   	/**
   	 * 
   	 * @return {String}
   	 */
   	getViewType: function (){return this.data.ViewType;},
   	
   	/**
   	 * 
   	 * @return {//TODO type}
   	 */
   	getLayoutFields: function (){return this.data.layoutFields;},
   	
   	/**
   	 * This method is called when a model is being listed in a List.
   	 * 
   	 * @return Text when info is been listing in a List view. 
   	 */
   	getTableDisplayValue: function (){
   		return this.getLayoutName();
   	}
});

/**
 * @class NG.models.Table
 * @extends Ext.data.Model
 * 
 * Represents FADs.
 */
NG.models.Table = Ext.regModel( "NG.models.Table", {
	
	/**
	 * Fields:<br>
	 * <li>tableName
	 * <li>tableDisplayName
	 * <li>tableVisibility
	 * <li>datasetType
	 * <li>isGeoTable
	 * <li>fieldList
	 * <li>layoutList
	 * <li>visibility: Boolean. Default value is <b>false</b>
	 * 
	 * @property fields
	 * @type Array
	 */
	fields: [ 
	          {name: 'tableName', type:'string'},
	          {name: 'tableDisplayName', type: 'string'},
	          {name: 'tableVisibility', type: 'boolean'},
	          {name: 'datasetType', type: 'string'},
	          {name: 'isGeoTable', type: 'boolean'},
	          {name: 'fieldList'},
	          {name: 'layoutList'},
	          {name: "visibility", defaultValue: false}
	         ],
	 /**
	  * @ignore
	  */
     phantom: true,
     
     /**
      * 
      * @return {String}
      */
     getTableName: function() {return this.data.tableName;},
     
     /**
      * 
      * @return {String}
      */
     getTableDisplayName: function() {return this.data.tableDisplayName;},
     
     /**
      * 
      * @return {Boolean}
      */
     getTableVisibility: function() {return this.data.tableVisibility;},
     
     /**
      * 
      * @return {String}
      */     
     getDatasetType: function() {return this.data.datasetType;},
     
     /**
      * 
      * @return {Boolean}
      */     
     isGeoTable: function() {return this.data.isGeoTable;},
     
     /**
      * 
      * @return {Array}
      */
     getFieldList: function() {return this.data.fieldList;},
     
     /**
      * 
      * @return {Array}
      */
     getLayoutList: function() {return this.data.layoutList;},
	 
     /**
      * This method return editable layout format supported by table (FAD).
      * 
      * @return {//TODO type} Structure supported by table (FAD)
      */
     getForm: function () {
    	 var layouts = this.getLayoutList();
    	 
    	 for ( var i = 0; i < layouts.length; i++) {
			if (layout[i].getTableDisplayValue() == "FORM") {
				return layout[i];
			}
			return null;
		}
     },
     
	/**
	 * This method is called when a model is being listed in a List.
	 * 
	 * @return {String} Text when table is being listed in a List view. 
	 */
	getTableDisplayValue: function (){
		return this.getTableName();
	},
	
	/**
	 * 
	 * @param {String} table
	 * @return {Boolean}
	 */
   	equalsTo: function (table) {
   		if (Ext.isEmpty(table) || Ext.isEmpty(table.getTableName())) {
   			return false;
   		}
   		return this.getTableName() === table.getTableName();
   	}
});
